<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class CheckGameWhiteAndPerson extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */

    protected $signature = 'CheckGameWhiteAndPerson';


    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '检查游戏白名单和个人池数据';


    // 时之祈愿
    protected $keyWheelWhite = 'CHECK:GAME:WHEEL:WHITE';//
    protected $keyWheelUser = 'CHECK:GAME:WHEEL:USER';
    protected $keyWheelExtract = 'CHECK:GAME:WHEEL:EXTRACT';
    // 扭蛋机
    protected $keyPoolWhite = 'CHECK:GAME:POOL:WHITE';
    protected $keyPoolUser = 'CHECK:GAME:POOL:USER';
    // 彩蛋白名单
    protected $keyColorWhite = 'CHECK:GIFT:COLOR:WHITE';
    // 地鼠白名单白名单
    protected $keySheepWhite = 'CHECK:GAME:SHEEP:WHITE';


    protected $robotUrl = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=d26f346f-6f30-4e81-baf6-14889fe3bc13';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        if(env('APP_ENV') != 'product'){
            return false;
        }


        // ====时之祈愿====
        // 白名单
        $wheelWhiteId = $this->getCacheId($this->keyWheelWhite);
        $wheelWhiteList = DB::table('wish_wheel_white')
            ->where('id', '>', $wheelWhiteId)
            ->orderBy('id', 'desc')
            ->get();
        $wheelWhiteList = $this->objToArray($wheelWhiteList);
        if ($wheelWhiteList) {
            foreach ($wheelWhiteList as $ww) {
                $content = sprintf('时之祈愿白名单新增：%s用户ID：%s，礼物ID：%s，管理员ID：%s，创建时间：%s', "\n", $ww['user_id'], $ww['gift_id'], $ww['admin_id'], $ww['created_at']);
                // 机器人
                $this->rebotMsg($content);
            }
            Redis::set($this->keyWheelWhite, $wheelWhiteList[0]['id']);
        }


        // 个人池
        $wheelUserId = $this->getCacheId($this->keyWheelUser);
        $wheelUserList = DB::table('wish_wheel_pool_user')
            ->where('id', '>', $wheelUserId)
            ->orderBy('id', 'desc')
            ->get();
        $wheelUserList = $this->objToArray($wheelUserList);
        if ($wheelUserList) {
            foreach ($wheelUserList as $ww) {
                $content = sprintf('时之祈愿个人池新增：%s用户ID：%s，奖池ID：%s，管理员ID：%s，创建时间：%s',"\n", $ww['user_id'], $ww['pool_id'], $ww['admin_id'], $ww['created_at']);
                // 机器人
                $this->rebotMsg($content);
            }
            Redis::set($this->keyWheelUser, $wheelUserList[0]['id']);
        }

        // 抽取池
        $wheelEXTRACTId = $this->getCacheId($this->keyWheelExtract);
        $wheelEXTRACTList = DB::table('wish_wheel_extract_pool_user')
            ->where('id', '>', $wheelEXTRACTId)
            ->orderBy('id', 'desc')
            ->get();
        $wheelEXTRACTList = $this->objToArray($wheelEXTRACTList);
        if ($wheelEXTRACTList) {
            foreach ($wheelEXTRACTList as $ww) {
                $content = sprintf('时之祈愿抽取池新增：%s用户ID：%s，抽取池ID：%s，管理员ID：%s，创建时间：%s',"\n",  $ww['user_id'], $ww['extract_pool_id'], $ww['admin_id'], $ww['created_at']);
                // 机器人
                $this->rebotMsg($content);
            }
            Redis::set($this->keyWheelExtract, $wheelEXTRACTList[0]['id']);
        }


        // ====扭蛋机====
        // 白名单
        $poolWhiteId = $this->getCacheId($this->keyPoolWhite);
        $poolWhiteList = DB::table('wish_pools_white')
            ->where('id', '>', $poolWhiteId)
            ->orderBy('id', 'desc')
            ->get();
        $poolWhiteList = $this->objToArray($poolWhiteList);
        if ($poolWhiteList) {
            foreach ($poolWhiteList as $ww) {
                $content = sprintf('扭蛋机白名单新增：%s用户ID：%s，礼物ID：%s，管理员ID：%s，创建时间：%s',"\n",  $ww['user_id'], $ww['gift_id'], $ww['admin_id'], $ww['created_at']);
                // 机器人
                $this->rebotMsg($content);
            }
            Redis::set($this->keyPoolWhite, $poolWhiteList[0]['id']);
        }

        // 个人池
        $poolUserId = $this->getCacheId($this->keyPoolUser);
        $poolUserList = DB::table('wish_pools_pool_user')
            ->where('id', '>', $poolUserId)
            ->orderBy('id', 'desc')
            ->get();
        $poolUserList = $this->objToArray($poolUserList);
        if ($poolUserList) {
            foreach ($poolUserList as $ww) {
                $content = sprintf('扭蛋机个人池新增：%s用户ID：%s，奖池ID：%s，管理员ID：%s，创建时间：%s',"\n",  $ww['user_id'], $ww['pool_id'], $ww['admin_id'], $ww['created_at']);
                // 机器人
                $this->rebotMsg($content);
            }
            Redis::set($this->keyPoolUser, $poolUserList[0]['id']);
        }


        // ====彩蛋====
        // 白名单
        $colorWhiteId = $this->getCacheId($this->keyColorWhite);
        $colorWhiteList = DB::table('color_gift_white_config')
            ->where('id', '>', $colorWhiteId)
            ->orderBy('id', 'desc')
            ->get();
        $colorWhiteList = $this->objToArray($colorWhiteList);
        if ($colorWhiteList) {
            foreach ($colorWhiteList as $ww) {
                $content = sprintf('彩蛋礼物白名单新增：%s用户ID：%s，礼物ID：%s，创建时间：%s',"\n",  $ww['user_id'], $ww['pop_gift_id'], $ww['created_at']);
                // 机器人
                $this->rebotMsg($content);
            }
            Redis::set($this->keyColorWhite, $colorWhiteList[0]['id']);
        }

        // ====地鼠====
        // 白名单
        $sheepWhiteId = $this->getCacheId($this->keySheepWhite);
        $sheepWhiteList = DB::connection('activity')->table('grasslandsheep_white')
            ->where('id', '>', $sheepWhiteId)
            ->orderBy('id', 'desc')
            ->get();
        $sheepWhiteList = $this->objToArray($sheepWhiteList);
        if ($sheepWhiteList) {
            foreach ($sheepWhiteList as $ww) {
                $content = sprintf('地鼠白名单新增：%s胜利赛道：%s，管理员ID：%s，创建时间：%s',"\n",  $ww['winner'], $ww['admin_id'], $ww['created_at']);
                // 机器人
                $this->rebotMsg($content);
            }
            Redis::set($this->keySheepWhite, $sheepWhiteList[0]['id']);
        }

        echo "success " . date('Y-m-d H:i:s');
    }

    public function rebotMsg($content) {
        $data = [
            'msgtype' => 'text',
            'text' => [
                'content' => $content
            ],
        ];

        return curl_request($this->robotUrl, $data, 'json', 'POST');
    }

    public function getCacheId($key) {
        $id = Redis::get($key);
        return $id ?: 0;
    }

    public function objToArray($document)
    {
        return json_decode(json_encode($document), true);
    }


}
